//
//  AXFTableViewController.m
//  AXFEightGroup
//
//  Created by ooops on 12/23/16.
//  Copyright © 2016 ya. All rights reserved.
//

#import "AXFDetailViewController.h"
#import "AXFDetailCell.h"
#import "AXFDetailSectionView.h"
#import "AXFDetailFooterView.h"
#import "AXFDataModel.h"
#import "AXFShoppingButtonView.h"
#import "AXFDetailShoppingBar.h"
#import "AXFShopViewController.h"
#import "AXFNavigationController.h"
#import "AXFTransitioningAnimator.h"

@interface AXFDetailViewController ()<UITableViewDelegate,UITableViewDataSource,AXFDetailShoppingBarButtonClickDelegate>

@property (nonatomic, strong) UIImageView *headImageView;                   /** 表头视图图片 */
@property (nonatomic, strong) UITableView *tableView;                       /** 表格 */
@property (nonatomic, strong) AXFDetailShoppingBar *bottomToolBar;                        /** 底部功能 */
@property (nonatomic, strong)   AXFShoppingButtonView *oprationShoppingCar; /** 购物车加减按钮 */
@property (nonatomic, strong) UILabel *lblBedge;                            /** 数量角标 */



@property (nonatomic, strong) AXFTransitioningAnimator *animator;


@end
static NSString *cellID = @"detail";
@implementation AXFDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.animator = [AXFTransitioningAnimator new];

    [self shareButton];
    [self setupTableView];
    [self setupBottomToolBar];
    [self setupNotifications];
    self.oprationShoppingCar.model = self.model;
}
#pragma mark - tableView配置
- (void)setupTableView {
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.bounces = NO;
    self.tableView.rowHeight = 142;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 30, 0);
    [self.view addSubview:self.tableView];
    UINib *nib = [UINib nibWithNibName:@"AXFDetailCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:cellID];
    self.tableView.tableHeaderView = self.headImageView;
}

#pragma mark - 底部工具栏
- (void)setupBottomToolBar {
    self.bottomToolBar = nibView(@"AXFDetailShoppingBar");
    self.bottomToolBar.delegate = self;
    
    [self.bottomToolBar addSubview:self.oprationShoppingCar];
    
    [self.oprationShoppingCar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.bottomToolBar);
        make.leading.mas_equalTo(100);
    }];
    
    
    [self.view addSubview:_bottomToolBar];
    [_bottomToolBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view);
        make.trailing.equalTo(self.view);
        make.bottom.equalTo(self.view);
        make.height.mas_equalTo(50);
    }];
}
#pragma mark - 购物车按钮
- (AXFShoppingButtonView *)oprationShoppingCar {
    if(_oprationShoppingCar == nil) {
        _oprationShoppingCar = nibView(@"AXFShoppingButtonView");
    }
    return _oprationShoppingCar;
}

#pragma mark - 购物车bedgelabel
- (UILabel *)lblBedge {
    if(_lblBedge == nil) {
        _lblBedge = [UILabel sj_createLabelWithText:@"" fontSize:15 blod:YES textColor:[UIColor whiteColor] backgroundColor:[UIColor redColor]];
    }
    return _lblBedge;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AXFDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    cell.model = self.model;
    return cell;
}

- (UIImageView *)headImageView {
    UIImageView *imgV = [[UIImageView alloc] init];//self.tableView.tableHeaderView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:]];
    imgV.image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:_model.imgURL];
    [imgV sizeToFit];
    return imgV;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return self.sectionView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 100.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1759.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return self.footerView;
}

/** 分享按钮 */
- (void)shareButton {
    UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithTitle:@"分享" style:UIBarButtonItemStylePlain target:self action:@selector(shareButtonClick:)];
    [shareButton setTintColor:[UIColor blackColor]];
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor blackColor]];
    self.navigationItem.rightBarButtonItem = shareButton;
}

- (void)shareButtonClick:(UIBarButtonItem *)sender {
    SNSLog(@"sender = %@",sender.title);
}


- (void)setModel:(AXFDataModel *)model {
    _model = model;
    
    self.sectionView = nibView(@"AXFDetailSectionView");
    self.footerView = nibView(@"AXFDetailFooterView");
    
    //[self.footerView.footerImageView sd_setImageWithURL:[NSURL URLWithString:model.imgURL] placeholderImage:[UIImage imageNamed:@"v2_placeholder_full_size"]];
    self.sectionView.nameLabel.text = model.name;
    self.sectionView.sallPriceLabel.text = [NSString stringWithFormat:@"$%@",model.partner_price];
    
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"$%@",model.market_price] attributes:attribtDic];
    [self.sectionView.marketPriceLabel setAttributedText:attribtStr] ;
}


#pragma mark - 配置监听购物车按钮操作通知
- (void)setupNotifications {
//    //id observer,  SEL sel, NSString *key
    NotificationCenterAddObserver(self, Action_AXFShoppingButtonView_Plus, @selector(plusClickedNotification:));
    NotificationCenterAddObserver(self, Action_AXFShoppingButtonView_Miuns, @selector(minusClickedNotification:));
    NotificationCenterAddObserver(self, Action_AXFShoppingButtonView_AmountChange, @selector(amountChange:));
}

///增加逻辑
- (void)plusClickedNotification:(NSNotification *)notification {
    self.oprationShoppingCar.model = self.model;
}
//减少逻辑
- (void)minusClickedNotification:(NSNotification *)notification {
    //self.oprationShoppingCar.model = self.model;
}
//购物车数量变化逻辑
- (void)amountChange:(NSNotification *)notification {
    //NSLog(@"总数量 = %ld",_ModelMgr.productTotalAmount);
    NSInteger totalAmont = _ModelMgr.productTotalAmount;
    NSString *bedge = [NSString stringWithFormat:@"%ld",totalAmont];
    [self.lblBedge setText:bedge];
    [self.lblBedge setFrame:CGRectMake(SCREEN_WIDTH - 30, SCREEN_HEIGHT - 80, 20, 20)];
    self.lblBedge.layer.cornerRadius = self.lblBedge.bounds.size.height * .5;
    self.lblBedge.layer.masksToBounds = YES;
    [self.view addSubview:self.lblBedge];
    self.lblBedge.hidden = totalAmont > 0 ? NO : YES;
    
}



#pragma mark - 购物车按钮点击方法
- (void)shoppingBarButtonClick:(UIButton *)sender {
    //[self.navigationController pushViewController:[AXFShopViewController new] animated:YES];
    
    UIViewController *vc = [AXFShopViewController new];
    
    AXFNavigationController *nav = [[AXFNavigationController alloc] initWithRootViewController:vc];
    nav.title = @"购物车";
    nav.modalPresentationStyle = UIModalPresentationCustom;
    nav.transitioningDelegate = self.animator;
    [self presentViewController:nav animated:YES completion:nil];
}

@end
